package com.peternguyen.totolistpro;

import java.util.Locale;

import com.peternguyen.totolistpro.R.string;
import com.peternguyen.totolistpro.dao.AccountDAO;
import com.peternguyen.totolistpro.dao.ConfigDAO;
import com.peternguyen.totolistpro.data.Account;
import com.peternguyen.totolistpro.data.Configapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	Button btnLogin;

	private Account acc;
	AccountDAO accDao;
	ConfigDAO confDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Change language
		confDao = new ConfigDAO(this);
		Configapp lang = confDao.getConfig("language");		
		String lstLang[] = {"en","vi"};
		int iLang = Integer.parseInt(lang.get_value());
		if(iLang > 0){
			Locale locale = new Locale(lstLang[iLang-1]);
			Locale.setDefault(locale);
			Configuration config = new Configuration();
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		}	
		
		setContentView(R.layout.activity_login);
		
		
		//Change language
//		Locale locale = new Locale("vi");
//		Locale.setDefault(locale);
//		Configuration config = new Configuration();
//		config.locale = locale;
//		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
//		
		// Create DAO object				
		accDao = new AccountDAO(LoginActivity.this);
		acc = accDao.getAccount();
//		if (acc.get_id() == 0) {
//			accDao.createAcount("123456", "itnguyennam@gmail.com");
//		}
		/*
		 * if (acc.get_id() == 0) { // Khong co mat khau
		 * setContentView(R.layout.activity_login);
		 * 
		 * } else {
		 */

		btnLogin = (Button) findViewById(R.id.btnlogin);
		btnLogin.setOnClickListener(this);

		// }

		/*
		 * btn_menu = (Button) findViewById(R.id.btn_menu);
		 * btn_menu.setOnClickListener(this);
		 */
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		int key = v.getId();
		switch (key) {
		case R.id.btnlogin:
			AlertDialog.Builder alert = new AlertDialog.Builder(this);

			alert.setTitle(getString(R.string.welcome_login_pw));
			alert.setMessage(getString(R.string.login_pw));

			// Set an EditText view to get user input
			final EditText inputpw = new EditText(this);
			alert.setView(inputpw);

			inputpw.setGravity(Gravity.CENTER);
			inputpw.setHint("Password");
			inputpw.setWidth(100);
			inputpw.setInputType(InputType.TYPE_CLASS_TEXT
					| InputType.TYPE_TEXT_VARIATION_PASSWORD);

			alert.setPositiveButton(getString(R.string.login_ok),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {

							Editable value = inputpw.getText();
							acc = accDao.getAccount();
							Log.v("PWD", acc.get_pwd());
							Log.v("PWD", value.toString());
							if (acc.get_pwd().equals(value.toString())) {
								Intent i = new Intent(LoginActivity.this,
										MainActivity.class);
								startActivity(i);
								LoginActivity.this.finish();
							} else {
								Toast.makeText(LoginActivity.this,
										getString(R.string.pw_valid), 0).show();
							}
						}
					});

			alert.setNegativeButton(getString(R.string.login_cancel),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							// Canceled.
						}
					});

			alert.show();

			break;
		/*
		 * case R.id.btn_menu: Toast.makeText(this, "Nut 1", 0).show(); break;
		 */
		default:
			break;
		}

	}
}
